/*
 * CommonMethod.java
 *
 * Created on 2007年4月23日, 下午2:49
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.ecomm.util;



import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 *
 * @author pijunliang
 */
public class Dtree {
  
  private ArrayList sRet = new ArrayList();
  /** Creates a new instance of CommonMethod */
  public Dtree() {
  }
  public ArrayList CreateDtree(String id){
    this.DTreeOrgAndUserIdForIM(id);
    return sRet;
  }
  /**
   *
   */
  public void DTreeOrgAndUserIdForIM(String id){
    
    if ("".equals(id)) id = "00000001";
    String  sSQL = "SELECT id,name,children,parentid FROM orgmodel_orginfo WHERE parentid='" + id + "' ORDER BY weight desc";
    DB db=null;
    
    try {
      if(db ==null)
        db= DBFactory.getDBInstance();
      PreparedStatement pst = null;
      pst = db.createPreparedStatement(sSQL);
      ResultSet rs = pst.executeQuery();
      while (rs.next()) {
        //如果是叶子节点
        if (rs.getInt("children") == 0) {
          sRet.add(rs.getString("id")+","+rs.getString("parentid")+","+rs.getString("name"));
          this.getuserIdBYOrgid(rs.getString("id"));
          // sRet.append("d.add('"+rs.getString("id")+"','"+rs.getString("parentid")+"','<a href=\"#\" onclick=\"opend(\'"+rs.getString("id")+"\')\" >"+rs.getString("name")+"</a>','','/chat/img/folclose.gif','/chat/img/folopen.gif');");
          //sRet.append(this.getuserIdBYOrgid(rs.getString("id")));
        }else{
          // sRet.append("d.add('"+rs.getString("id")+"','"+rs.getString("parentid")+"','<a href=\"#\" onclick=\"opend(\'"+rs.getString("id")+"\')\" >"+rs.getString("name")+"</a>','','/chat/img/folclose.gif','/chat/img/folopen.gif');");
          // sRet.append(this.getuserIdBYOrgid(rs.getString("id")));
          // sRet.append(this.DTreeOrgAndUserIdForIM(rs.getString("id")));
          sRet.add(rs.getString("id")+","+rs.getString("parentid")+","+rs.getString("name"));
          this.DTreeOrgAndUserIdForIM(rs.getString("id"));
        }
      }
      if (rs!=null) {
        rs.close();
        rs = null;
      }
      pst.close();
      pst = null;
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if(db!=null)
        db.close();
    }
  }
  /**
   *SELECT info.id,info.name
   * FROM orgmodel_usertoorg orguser ,orgmodel_userinfo info
   * WHERE orguser.orgid = 'orgacebcafa88936' and orguser.userid = info.id
   */
  public void getuserIdBYOrgid(String orgid){
    String  sSQL = "SELECT info.id,info.name FROM orgmodel_usertoorg orguser ,orgmodel_userinfo info WHERE orguser.orgid = '"+orgid+"' and orguser.userid = info.id ";
    DB db=null;
    
    try {
      if(db ==null)
        db= DBFactory.getDBInstance();
      PreparedStatement pst = null;
      pst = db.createPreparedStatement(sSQL);
      ResultSet rs = pst.executeQuery();
      while (rs.next()) {
        sRet.add(rs.getString("id")+","+orgid+","+rs.getString("name"));
        // sRetUser.append("d.add('"+rs.getString("id")+"','"+orgid+"','<a href=\"#\" onclick=\"opend(\'"+rs.getString("id")+"\')\" >"+rs.getString("name")+"</a>','','/chat/img/folclose.gif','/chat/img/folopen.gif');");
      }
      if (rs!=null) {
        rs.close();
        rs = null;
      }
      pst.close();
      pst = null;
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if(db!=null)
        db.close();
    }
  }
  
}
